package cn.kgc.controller;

import cn.kgc.common.constant.Constants;
import cn.kgc.pojo.UserInf;
import cn.kgc.service.UserService;
import cn.kgc.utils.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    private  static Logger logger = Logger.getLogger(UserController.class);
    @Resource
    UserService userService;

    @RequestMapping({"/"})
    public String toLogin(){
        return "login";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam("loginname")String loginname,
                        @RequestParam("PASSWORD")String PASSWORD,
                        HttpServletRequest request,
                        Model model){
        UserInf userInf = userService.getUserInfo(loginname,PASSWORD);
        if (userInf != null){
            HttpSession session = request.getSession();
            session.setAttribute(Constants.USER_SESSION,userInf);
            return "redirect:/user/asd";
        }else {
            model.addAttribute("error","用户名或者密码错误");
            return "login";
        }
    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "redirect:/user/";
    }
    @RequestMapping("/asd")
    public String asd(){
        return "index";
    }
    @RequestMapping("/sy")
    public String sy(Model model){
        List<UserInf> list = userService.getUser();
        model.addAttribute("userinf",list);
        return "shouye";
    }


    @RequestMapping("/limit")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "status",required = false) Integer status,
                        @RequestParam(value = "username",required = false) String username,
                        Model model){

        PageInfo pageInfo = new PageInfo();
        if (currentPageNo !=null){
            pageInfo.setCurrentPageNo(currentPageNo);
        }
        if (pageSize!=null){
            pageInfo.setPageSize(pageSize);
        }
        int totalCount = userService.getCount(status,username);
        pageInfo.setTotalConut(totalCount);
        List<UserInf> list =userService.getUserByPage(pageInfo.getCurrentPageNo(),pageInfo.getPageSize(),status,username);
        pageInfo.setData(list);
        model.addAttribute("user",pageInfo);
        model.addAttribute("status",status);
        model.addAttribute("username",username);

        return "userLimit";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("id")Integer id,Model model){
        UserInf userInf = userService.getUserById(id);
        model.addAttribute("user",userInf);
        return "userUpdate";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(UserInf userInf){
        boolean flag = userService.updateUser(userInf);
        if (flag){
            return "redirect:/user/limit";
        }else {
            return "userUpdate";
        }
    }
    @RequestMapping("/toDel")
    public String toDel(@RequestParam("id")Integer id){
        boolean flag = userService.delId(id);
        if (flag){
            return "redirect:/user/limit";
        }else {
            return "userLimit";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "userAdd";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(UserInf userInf){
        boolean flag = userService.addUser(userInf);
        if (flag){
            return "redirect:/user/limit";
        }else {
            return "userAdd";
        }
    }


    @RequestMapping("/userIsExist/{loginname}")
    @ResponseBody
    public JSONObject userIsExist(@PathVariable String loginname) {

        UserInf userInf = userService.getUserloginname(loginname);
        JSONObject jsonObject = new JSONObject();
        if (userInf == null) {
            jsonObject.put("code", "10000");
            jsonObject.put("msg", "可以使用");
        } else {
            jsonObject.put("code", "10001");
            jsonObject.put("msg", "不可以使用");
        }
        return jsonObject;
    }
}
